﻿using System;
using System.Windows.Forms;
using TicTacToeControl;

namespace TicTacToeDisplay
{
    public partial class Form1 : Form
    {

        String playerOne = "Mike";
        String playerTwo = "Erin";
        State gameState;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            gameState = new State(playerOne, playerTwo);

            if (gameState.IsPlayerOneTurn == true)
                lblPlayerTurn.Text = "It's " + playerOne + "'s Turn";
            else
                lblPlayerTurn.Text = "It's " + playerTwo + "'s Turn";
        }

        private void btn00_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(0, 0))
            {
                if (gameState.IsPlayerOneTurn)
                    btn00.Text = "X";
                else
                    btn00.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn10_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(1, 0))
            {
                if (gameState.IsPlayerOneTurn)
                    btn10.Text = "X";
                else
                    btn10.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn20_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(2, 0))
            {
                if (gameState.IsPlayerOneTurn)
                    btn20.Text = "X";
                else
                    btn20.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn01_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(0, 1))
            {
                if (gameState.IsPlayerOneTurn)
                    btn01.Text = "X";
                else
                    btn01.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn11_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(1, 1))
            {
                if (gameState.IsPlayerOneTurn)
                    btn11.Text = "X";
                else
                    btn11.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn21_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(2, 1))
            {
                if (gameState.IsPlayerOneTurn)
                    btn21.Text = "X";
                else
                    btn21.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn02_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(0, 2))
            {
                if (gameState.IsPlayerOneTurn)
                    btn02.Text = "X";
                else
                    btn02.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn12_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(1, 2))
            {
                if (gameState.IsPlayerOneTurn)
                    btn12.Text = "X";
                else
                    btn12.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }

        private void btn22_Click(object sender, EventArgs e)
        {
            if (gameState.PlacePiece(2, 2))
            {
                if (gameState.IsPlayerOneTurn)
                    btn22.Text = "X";
                else
                    btn22.Text = "O";

                if (gameState.HaveWinner)
                    MessageBox.Show("We have a winner");
                if (!gameState.MoreMovesAval)
                    MessageBox.Show("No more moves allowed");
            }
            else
                MessageBox.Show("That is not a legal move. Please try again.");
        }



    }
}
